<?php

    ##################################################
    #
    # Copyright (c) 2012 - ABSoft
    # Written and Designed by Immanuel192 
    #
    # This file is part of Exponent
    #
    # GPL: http://www.gnu.org/licenses/gpl.txt
    #
    ##################################################

    if (!defined('EXPONENT')) exit('');

    /**
    * Navigation Page select control
    * Used when create new navigation page
    * to link new page to exist page
    *
    * @package Subsystems
    * @subpackage Forms
    */

    require_once(BASE."subsystems/forms/controls/formcontrol.php");

    class navlinkcontrol extends formcontrol {
        var $size = 0;
        var $maxlength = "";
        var $caption = "";

        function name() { return "Navigation control to link new page to exist page"; }
        function isSimpleControl() { return true; }
        function useGeneric() { return false; }
        function getFieldDefinition() {
            return array(
                DB_FIELD_TYPE=>DB_DEF_STRING,
                DB_FIELD_LEN=>512);
        }

        function navlinkcontrol($default = "", $size = 0, $disabled = false, $maxlength = 0, $filter = "", $required = false) {
            $this->default = $default;
            $this->size = $size;
        }

        function controlToHTML($name) {
            // phát sinh ra 1 ID random
            $id_random="ID".(rand() % 100); // giới hạn 100 số
            // javascript
            $html="
            <script language='javascript'>
                var current_lang = '".LANG."';
                jQuery(document).ready(function(){
                    jQuery('select#language').change(function(){
                        current_lang = jQuery(this).val();
                        jQuery('#l{$name}').val(jQuery(this).val());
                    });
                    jQuery('#l{$name}').change(function(){
                        if (jQuery(this).val() == current_lang)
                        {
                            jQuery('select#{$name}').html('<option value=\"\">Dont link</option>');
                        }
                        else
                        {
                            // ajax to server to get page list
                            var t=Math.random();
                            jQuery.post('/index.php?module=navigationmodule&action=navlink_listpage_bylang&ajax_action=1&language='+encodeURIComponent(jQuery(this).val())+'&t='+t, function(data) {
                                
                                jQuery('select#{$name}').html(data);
                            });
                        }
                    });
                });
            </script>
            ";
            
            // add language control
            $lang =exponent_lang_list(); 
            $html .= '<select class="select" id="l' . $name . '" name="l' . $name . '" size="' . $this->size . '" style="display: inline;">';
            foreach ($lang as $kl=>$vl)
            {
                $html.="<option value='{$kl}' ";
                if ($kl == LANG)
                    $html.=" selected";
                $html.=" >{$vl}</option>";
            }
            $html.='</select>';
            
            $html .= '<select class="select" id="' . $name . '" name="' . $name . '" size="' . $this->size . '"><option value="">Dont link</option></select>';
            return $html;
        }

        function form($object) {
            if (!defined("SYS_FORMS")) require_once(BASE."subsystems/forms.php");
            exponent_forms_initialize();

            $form = new form();
            if (!isset($object->identifier)) {
                $object->identifier = "";
                $object->caption = "";
                $object->default = "";
                $object->size = 0;
                $object->maxlength = 0;
                $object->required = false;
            }
            $i18n = exponent_lang_loadFile('subsystems/forms/controls/textcontrol.php');

            $form->register("identifier",$i18n['identifier'],new textcontrol($object->identifier));
            $form->register("caption",$i18n['caption'], new textcontrol($object->caption));
            $form->register("default",$i18n['default'], new textcontrol($object->default));
            $form->register("size",$i18n['size'], new textcontrol((($object->size==0)?"":$object->size),4,false,3,"integer"));
            $form->register("maxlength",$i18n['maxlength'], new textcontrol((($object->maxlength==0)?"":$object->maxlength),4,false,3,"integer"));
            $form->register(null, null, new htmlcontrol('<br />'));
            $form->register("required", $i18n['required'], new checkboxcontrol($object->required,true));
            $form->register(null, null, new htmlcontrol('<br />'));
            $form->register("submit","",new buttongroupcontrol($i18n['save'],'',$i18n['cancel']));
            return $form;
        }

        function update($values, $object) {
            if ($object == null) $object = new textcontrol();
            if ($values['identifier'] == "") {
                $i18n = exponent_lang_loadFile('subsystems/forms/controls/textcontrol.php');
                $post = $_POST;
                $post['_formError'] = $i18n['id_req'];
                exponent_sessions_set("last_POST",$post);
                return null;
            }
            $object->identifier = $values['identifier'];
            $object->caption = $values['caption'];
            $object->default = $values['default'];
            $object->size = intval($values['size']);
            $object->maxlength = intval($values['maxlength']);
            $object->required = isset($values['required']);
            return $object;
        }

    }

?>
